package Stack;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 姚东名
 * Date: 2024-09-02
 * Time: 19:26
 */
public class Test {
    //出栈入栈次序匹配：
    public boolean IsPopOrder (int[] pushV, int[] popV) {
        // write code here
        Stack<Integer> stack = new Stack<>();
        int j = 0;
        for (int i = 0; i < pushV.length; i++) {
            stack.push(pushV[i]);
            while (!stack.isEmpty() && j < popV.length && stack.peek() == popV[j]) {
                stack.pop();
                j++;
            }
        }
        return stack.isEmpty();
    }

    //逆波兰表达式求值：
    public int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for(String x : tokens) {
            if (!isOperator(x)) {
                //tokens = ["2","1","+","3","*"]
                int val = Integer.parseInt(x);//字符串转整型
                stack.push(val);
            } else {
                int val2 = stack.pop();//拿出的第一个放右边
                int val1 = stack.pop();//第二个放左边
                switch(x) {
                    case "+":
                        stack.push(val1 + val2);
                        break;
                    case "-":
                        stack.push(val1 - val2);
                        break;
                    case "*":
                        stack.push(val1 * val2);
                        break;
                    case "/":
                        stack.push(val1 / val2);
                        break;
                }
            }
        }
        return stack.peek();
    }
    private boolean isOperator(String str) {
        if (str.equals("+") || str.equals("-") || str.equals("*") || str.equals("/")) {
            return true;
        }
        return false;
    }

    //括号匹配：
    public boolean isValid(String s) {
        // 匹配 不匹配 左括号多 右括号多：
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            // 把'('，'{'，'[' 左括号放到栈中：
            if (ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            } else {
                // 开始匹配：
                if (stack.isEmpty()) {
                    return false;
                }
                char ch2 = stack.peek();
                if (ch == ')' && ch2 == '(' || ch == '}' && ch2 == '{' || ch == ']' && ch2 == '[') {
                    stack.pop();
                } else {
                    return false;
                }
            }
        }
        if (!stack.isEmpty()) {
            return false;
        }
        return true;
    }
    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        stack.push(11);//入栈：
        stack.push(21);
        stack.push(21);
        stack.push(31);
        stack.push(41);

        System.out.println(stack.peek());
        System.out.println(stack.pop());//出栈了
        System.out.println(stack.peek());
    }
    public static void main1(String[] args) {
        MyStack myStack = new MyStack();
        myStack.push(11);
        myStack.push(21);
        myStack.push(21);
        myStack.push(31);
        myStack.push(41);

        System.out.println(myStack.pop());
        System.out.println(myStack.peek());
    }
}
